//
//  BillingDetailsViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/25.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "BillingDetailsViewController.h"
#import "BillingDetailsApi.h"
#import "BillingDetailsTableViewCell.h"

@interface BillingDetailsViewController ()

@end

@implementation BillingDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账户明细";
    [self addMj_footer];
    [self addMj_header];
    self.tableView.bounces = NO;
    
    [self.tableView registerCellWithNib:@"BillingDetailsTableViewCell"];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData {
    if (!self.listApi) {
        self.listApi = [BillingDetailsApi new];
    }
    WeakSelf(self)
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            NSArray *arr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *dic in arr) {
                BillingDetails *obj = [[BillingDetails alloc]init];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
            }
            [weakself.tableView reloadData];
        }
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BillingDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BillingDetailsTableViewCell" forIndexPath:indexPath];
    BillingDetails *billing = self.dataArray[indexPath.row];
    
    NSString *type;
    if (billing.type == 1) {
        type = @"图文咨询";
    }else if (billing.type == 2){
        type = @"语音咨询";
    }else if (billing.type == 3){
        type = @"视频咨询";
    }else if (billing.type == 4){
        type = @"面对面咨询";
    }
    
    cell.typeLabel.text = type;
    cell.time.text = billing.created_at;
    cell.money.text = [NSString stringWithFormat:@"+%.0f",billing.money];
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
